package com.fuershiye.woman.monitor.controller;

import com.fuershiye.woman.monitor.beans.*;
import com.fuershiye.woman.monitor.constants.Massage;
import com.fuershiye.woman.monitor.model.Department;
import com.fuershiye.woman.monitor.service.*;
import com.fuershiye.woman.monitor.utils.DateFormatUtil;
import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping(value = "/indicator/count")
public class IndicatorCountController {

	private static final Logger logger = LoggerFactory
			.getLogger(IndicatorCountController.class);

	@Autowired
	IndicatorTypeService indicatorTypeService;
	@Autowired
	IndicatorService indicatorService;
	@Autowired
	DepartmentService departmentService;
	@Autowired
	DepartIndicatorService departIndicatorService;

	/**
	 * 获取省级各部门指标的数量
	 * 
	 * 适用场景【省级部门年度分析】
	 */
	//	@RequiresPermissions("indicator:list")
	@RequestMapping(value = "/province.do")
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@ResponseBody
	public Result countProvince(Long formsid, Integer year) {

		if(!DateFormatUtil.isYear(year)){
			return new Result(Massage.ERROR_YEAR);
		}
		if(formsid==null||formsid==0){
			return new Result(Massage.FORMS_IS_NULL);

		}
		// 未删除状态数据
		int status = 1;
		// 部门级别 1:国家级;2:省/部级;3:地级市;4:区县级;5:乡镇级;6:其他
		int level = 2;


		// 查找所有的部门
		List<Department> departmentList = departmentService.list("", null, level, status, new Page(1, Integer.MAX_VALUE));
		if (departmentList.isEmpty()) {
			return new Result(Massage.DEPART_IS_NULL);
		}

		int all = 0;
    	List<ViewNameCount> result = Lists.newArrayList();
		for(Department department:departmentList){
			if(department==null || department.getId()==null ||department.getId() ==0){
				continue;
			}
			ViewNameCount vdi = new ViewNameCount();
			vdi.setId(department.getId());
			vdi.setName(department.getSname());

			int count = departIndicatorService.countInCondition(department.getId(), formsid, null, year, 1);
			vdi.setCount(count);
			
			result.add(vdi);
			
			all = all + count;
		}
		
		return new Result(Massage.SUCCESS, new PageResult(result, all));
	}
	
	/**
	 * 获取各地域下部门指标的数量
	 *
	 * level 部门级别 1:国家级;2:省/部级;3:地级市;4:区县级;5:乡镇级;6:其他
	 * 适用场景【省级部门年度分析】
	 */
	//	@RequiresPermissions("indicator:list")
	@RequestMapping(value = "/area.do")
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@ResponseBody
	public Result countArea(Long areaid, Long formsid, Integer level, Integer year) {
		
		if(areaid==null || areaid<1){
			return new Result(Massage.ERROR_PARAMETER);
		}

		if(!DateFormatUtil.isYear(year)){
			return new Result(Massage.ERROR_YEAR);
		}

		// 查找所有的部门
		List<IdNameSname> idNameSnameList = departmentService.getAllByLevelAndArea(level, areaid, 1);
		if (idNameSnameList.isEmpty()) {
			return new Result(Massage.DEPART_IS_NULL);
		}
		
		int all = 0;
		List<ViewNameCount> result = Lists.newArrayList();
		for(IdNameSname idNameSname:idNameSnameList){
			if(idNameSname==null || idNameSname.getId()==null ||idNameSname.getId() ==0){
				continue;
			}
			ViewNameCount vdi = new ViewNameCount();
			vdi.setId(idNameSname.getId());
			vdi.setName(idNameSname.getSname());
			int count = departIndicatorService.countInCondition(idNameSname.getId(), formsid, null, year, 1);
			vdi.setCount(count);
			result.add(vdi);
			
			all = all + count;
		}
		
		return new Result(Massage.SUCCESS, new PageResult(result, all));
	}
	
	/**
	 * 各领域检测指标占所有监测指标的数量
	 * 
	 * 适用场景【省级部门年度分析】
	 */
	
	//	@RequiresPermissions("indicator:list")
	@RequestMapping(value = "/monitor.do")
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@ResponseBody
	public Result countMonitor(Long formsid) {
		
		// 未删除状态数据
		int status = 1;
		// 获取监测指标
		int province =1;
		
		//所有监测指标的数量
		int all = indicatorService.getTotal(province, status);

		List<IdName> indicatorTypeList = indicatorTypeService.getByCondition(formsid, "", status);
		if(indicatorTypeList==null || indicatorTypeList.size() <1){
			return new Result(Massage.INDICATOR_IS_NULL);
		}
		
		List<ViewNameCount> result = Lists.newArrayList();
		for(IdName idName:indicatorTypeList){
			if(idName==null || idName.getId()==null ||idName.getId() ==0){
				continue;
			}
			ViewNameCount vdi = new ViewNameCount();
			vdi.setId(idName.getId());
			vdi.setName(idName.getName());

			int count = indicatorService.getTotal(idName.getId(), status);
			vdi.setCount(count);
			result.add(vdi);
		}
		
		return new Result(Massage.SUCCESS, new PageResult(result, all));
	}
	
	/**
	 * 各领域指标的数量
	 * 
	 */
	
	//	@RequiresPermissions("indicator:list")
	@RequestMapping(value = "/type.do")
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@ResponseBody
	public Result countType(Long formsid) {
		
		// 未删除状态数据
		int status = 1;
		
		//所有监测指标的数量
		int all = indicatorService.getTotalByCondition(formsid, status);
		
		List<IdName> indicatorTypeList = indicatorTypeService.getByCondition(formsid, "", status);
		if(indicatorTypeList==null || indicatorTypeList.size() <1){
			return new Result(Massage.INDICATOR_IS_NULL);
		}
		
		List<ViewNameCount> result = Lists.newArrayList();
		for(IdName idName:indicatorTypeList){
			if(idName==null || idName.getId()==null ||idName.getId() ==0){
				continue;
			}
			ViewNameCount vdi = new ViewNameCount();
			vdi.setId(idName.getId());
			vdi.setName(idName.getName());
			
			int count = indicatorService.getTotal(formsid, idName.getId(), status);
			vdi.setCount(count);
			result.add(vdi);
		}
		
		return new Result(Massage.SUCCESS, new PageResult(result, all));
	}

}
